package com.ccp.dev.qbdms.ws.service;

import com.ccp.dev.core.basic.base.BaseService;
import com.ccp.dev.core.basic.util.UUIDUtils;
import com.ccp.dev.core.util.BeanUtils;
import com.ccp.dev.qbdms.ws.dao.CodeZsjDao;
import com.ccp.dev.qbdms.ws.model.CodeZsj;
import com.ccp.dev.qbdms.ws.util.ReturnZsjUtil;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Iterator;
import java.util.List;

@Service
public class CodeZsjService extends BaseService<CodeZsj> {
	static Logger logger = LoggerFactory.getLogger(CodeZsjService.class);

	@Resource
	private CodeZsjDao codeZsjDao;

	/**
	 * @descrip 解析xml，封装成组织codeZsj
	 * @param xmlValue
	 * @return
	 * @throws Exception
	 */
	public String parseXMLStr(String xmlValue){

		Document doc = null;
		String code ="";

		try {
			// 将字符串转为XML
			doc = DocumentHelper.parseText(xmlValue);
		} catch (DocumentException e) {
			e.printStackTrace();
			return ReturnZsjUtil.returnXmlStr(code, "e", "解析xml出现错误!");
		} 
		
		Element rootElt = doc.getRootElement(); // 获取根节点
		Iterator iter = rootElt.elementIterator("item"); // 获取根节点下的子节点item
		// 遍历item节点
		while (iter.hasNext()){
			Element recordEle = (Element) iter.next();
			//代码主数据
			code = recordEle.elementTextTrim("code"); // 拿到head节点下的子节点code值
			// 操作标识符
			String flag = recordEle.elementTextTrim("flag"); // 操作标识符 1：代表插入  4：代表更新  3：代表删除
			logger.info("flag="+flag + "  code="+code);
			if (code == null) {
				return ReturnZsjUtil.returnXmlStr(code, "e", "人员代码为空!");
			}

			//封装codeZsj
			CodeZsj codeZsj = null;
			try {
				codeZsj = parseToCodeZsj(recordEle);
			} catch (Exception e) {
				e.printStackTrace();
				return ReturnZsjUtil.returnXmlStr(code, "e", "xml对象字段解析错误!");
			}

			if ("4".equals(flag)){
				// 更新修改
				List<CodeZsj> codeZsjList = codeZsjDao.getByCode(code);
				if(BeanUtils.isNotEmpty(codeZsjList) && codeZsjList.size() > 0){
					CodeZsj queryByrydm = codeZsjList.get(0);
					if(BeanUtils.isNotEmpty(queryByrydm)) {
						codeZsjDao.update(codeZsj);
						logger.info("更新代码主数据code="+code);
					}
				}else{
					codeZsj.setId(UUIDUtils.getUUIDFor32());
					codeZsjDao.add(codeZsj);
					logger.info("插入代码主数据code="+code);
				}


			}
			if ("3".equals(flag)){
				// 删除 停用
				List<CodeZsj> codeZsjList = codeZsjDao.getByCode(code);
				if(BeanUtils.isNotEmpty(codeZsjList) && codeZsjList.size() > 0) {
					CodeZsj queryByrydm = codeZsjList.get(0);
					if (BeanUtils.isNotEmpty(queryByrydm)) {
						codeZsjDao.update(codeZsj);
					}
				}
			}
		}

		//成功
		return ReturnZsjUtil.returnXmlStr(code, "s", "");


	}

	private CodeZsj parseToCodeZsj(Element recordEle) throws ParseException {
		// TODO Auto-generated method stub
		CodeZsj codeZsj = new CodeZsj();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		//代码主数据代码
		codeZsj.setCode(recordEle.elementTextTrim("code"));
		//代码主数据名称
		codeZsj.setName(recordEle.elementTextTrim("name"));
		//哪一类代码主数据
		codeZsj.setType(recordEle.elementTextTrim("type"));
		//操作标识符
		codeZsj.setFlag(recordEle.elementTextTrim("flag"));
		//传输日期
		codeZsj.setTranstime(BeanUtils.isNotEmpty(recordEle.elementTextTrim("transtime")) ? sdf.parse(recordEle.elementTextTrim("transtime")) : null);
		
		return codeZsj;
	}

	public List<CodeZsj> getByCodeName(String codeName, String inputVal){
		List<CodeZsj> byCodeName = codeZsjDao.getByCodeName(codeName,inputVal);
		return byCodeName;
	}
}
